Hello,
I'm writing syntax to replace all variable names of the last panel wave with the variable names of the actual panel wave in a large dofile.
I have a stata file holding 'oldvar' and 'newvar'. The text of the dofile has been transferred into a strL-variable with:
If there were only "normal" variable names to be replaced, it was an easy job, that can be done with
while looping over all varold/varnew-pairs.
But the variable names contain running indexes,which in the dofile-text are represented by a local macro that changes its value according to the loop index.
Here is an excerpt from the dofile that I want to update to clarify this:
The problem I'm not able to solve is how to construct a macro that contains exactly (for instance) the string "bfpm_l`iu'_1802"
The oldvar/newvar table represents the placeholders within the variable names by "XX".
So I have to construct a string holding the part of the variable name before `iu', plus `iu' plus the part after ìu'
The part before `iu' is captured by:
The part after `iu' is captured by:
But how to capture "`iu'" and construct `varold' by combining `ostr1' `placeholder'`ostr2 ???
I've tried all versions I can think of, including compound quotes and escape sign \ but nothing seemed to work.
I hope very much someone can help
Thanks, Klaudia
I'm writing syntax to replace all variable names of the last panel wave with the variable names of the actual panel wave in a large dofile.
I have a stata file holding 'oldvar' and 'newvar'. The text of the dofile has been transferred into a strL-variable with:
Code:
generate strL odofile = fileread(`"`pfad1s'/`synold'"') replace odofile = "" if _n > 1
Code:
replace odofile = `u'subinstr(odofile, `"`varold'"', `"`varnew'"', .) if _n == 1
But the variable names contain running indexes,which in the dofile-text are represented by a local macro that changes its value according to the loop index.
Here is an excerpt from the dofile that I want to update to clarify this:
Code:
local n = 15 forvalues i = 2(1)`n' { /* Generation of the variable parts of the variable names */ local j = `i'-1 if `i' < 10 { local in 0`i' } if `i' >= 10 { local in `i' } if `j' < 10 { local iu 0`j' } if `j' >= 10 { local iu `j' } use "filename", clear keep hhnr persnr welle bfpm_monin bfpm_l`iu'_1801 bfpm_l`iu'_1802 bfpm_l`iu'_19 bfpm_l`iu'_20 bfpm_l`iu'_21 /// bfpm_l`iu'_22
The oldvar/newvar table represents the placeholders within the variable names by "XX".
So I have to construct a string holding the part of the variable name before `iu', plus `iu' plus the part after ìu'
The part before `iu' is captured by:
Code:
local ostr1 = `u'substr(`"`varold'"',1,6)
Code:
local ostr2 = `u'substr(`"`varold'"',9,`u'strlen(`"`varold'"')-8
I've tried all versions I can think of, including compound quotes and escape sign \ but nothing seemed to work.
I hope very much someone can help
Thanks, Klaudia
Comment